﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using System.Collections;
using Office = Microsoft.Office.DocumentManagement.DocumentSets;
using MS.InformationManagement.Support;

namespace MS.InformationManagement.Core.Expediente
{
    public class ExpedienteManagement
    {

        #region fields

        MS.InformationManagement.DAL.DocumentCenter.Expediente _expediente;

        #endregion

        #region construnctors

        public ExpedienteManagement(MS.InformationManagement.DAL.DocumentCenter.Expediente expediente)
        {
            _expediente = expediente;
        }

        #endregion

        #region Public Methods

        public Office.DocumentSet CreateExpediente(string currentUser, string parentRadId)
        {
            string targetLibrary = PeopleHelper.GetPropertyUserProfile(GlobalConstants.UP_DEPARTMENT, currentUser);
            SPList userList = SPContext.Current.Web.Lists.TryGetList(targetLibrary);
            if (userList == null)
                throw new Exception(string.Format(ExceptionConstants.ERROR_BIBLIOTECA_USUARIO_INEXISTENTE, targetLibrary));
            SPContentTypeId contentType = userList.ContentTypes[GlobalConstants.CTTN_EXPEDIENTE].Id;
            string tempName = Guid.NewGuid().ToString();
            var CurrentDocumentSet = Office.DocumentSet.Create(userList.RootFolder, tempName, contentType, new Hashtable());
            string expedienteId = parentRadId + "-E";
            CurrentDocumentSet.Folder.Item[CustomSiteColumns.FIELD_DOCSET_NAME] = expedienteId;
            CurrentDocumentSet.Folder.Item[CustomSiteColumns.FIELD_EXPEDIENTE_ID] = expedienteId;
            CurrentDocumentSet.Folder.Item[CustomSiteColumns.FIELD_DOCSET_DESCRIPCION] = _expediente.Asunto;
            //CurrentDocumentSet.Folder.Item[CustomSiteColumns.FIELD_EXPEDIENTE_KIND] = TermStoreHelper.GetTaxonomyFieldByTerm(GlobalConstants.METADATA_GROUP_NAME, GlobalConstants.METADATA_TERM_TIPOS, GlobalConstants.METADATA_TERM_TIPO_EXPEDIENTE); ;
            CurrentDocumentSet.Folder.Item[CustomSiteColumns.FIELD_EXPEDIENTE_CODIGO] = _expediente.Código;
            CurrentDocumentSet.Folder.Item[CustomSiteColumns.FIELD_EXPEDIENTE_SUBSERIE] = _expediente.Subserie;
            CurrentDocumentSet.Folder.Item[CustomSiteColumns.FIELD_EXPEDIENTE_SUBJECT] = _expediente.Asunto;
            CurrentDocumentSet.Folder.Item[CustomSiteColumns.FIELD_EXPEDIENTE_START_FOLIO] = _expediente.FolioInicial;
            CurrentDocumentSet.Folder.Item[CustomSiteColumns.FIELD_EXPEDIENTE_LOCATION] = _expediente.UbicaciónFisica;
            CurrentDocumentSet.Folder.Item[CustomSiteColumns.FIELD_EXPEDIENTE_LOCATION_KIND] = _expediente.TipoUbicación;
            CurrentDocumentSet.Folder.Item[CustomSiteColumns.FIELD_EXPEDIENTE_ASSIGNED] = _expediente.UsuarioAsignado;
            CurrentDocumentSet.Folder.Item[CustomSiteColumns.FIELD_EXPEDIENTE_START] = _expediente.FechaInicial;
            CurrentDocumentSet.Folder.Item[CustomSiteColumns.FIELD_EXPEDIENTE_END] = _expediente.FechaFinal;
            CurrentDocumentSet.Folder.Item.Update();
            CurrentDocumentSet.Folder.Update();

            //Get updated documentset

            CurrentDocumentSet = Office.DocumentSet.GetDocumentSet(userList.GetItemById(CurrentDocumentSet.Item.ID).Folder);

            return CurrentDocumentSet;
        }


        #endregion


    }
}
